#include "../mqserver/mq_queue.hpp"
#include <gtest/gtest.h>

pprmq::QueueManager::ptr mqp;

class QueueTest : public testing::Environment
{
public:
    virtual void SetUp() override
    {
        mqp = std::make_shared<pprmq::QueueManager>("./data/test.db");
        // std::cout << "单元测试执行前的环境初始化" << std::endl;
    }
    virtual void TearDown() override
    {
        // mqp->clear();
        std::cout << "单元测试执行完毕后的环境清理" << std::endl;
    }
};

TEST(exchange_test,insert_test)
{
    google::protobuf::Map<std::string, std::string> map;
    map.insert({"k1","v1"});
    map.insert({"k2","v2"});
    mqp->declareQueue("queue1",true,false,false,map);
    mqp->declareQueue("queue2",true,false,false,map);
    mqp->declareQueue("queue3",true,false,false,map);
    mqp->declareQueue("queue4",true,false,false,map);
    ASSERT_EQ(mqp->size(),4);
}

TEST(exchange_test,select_test)
{
    ASSERT_EQ(mqp->exists("queue1"),true);
    ASSERT_EQ(mqp->exists("queue2"),true);
    ASSERT_EQ(mqp->exists("queue3"),true);
    ASSERT_EQ(mqp->exists("queue4"),true);

    pprmq::MsgQueue::ptr qup = mqp->selectMsgQueue("queue1");
    ASSERT_NE(mqp.get(),nullptr); // 这个智能指针已经指向了一个有效对象，不是空指针
    ASSERT_EQ(qup->_name,"queue1");
    ASSERT_EQ(qup->_durable,true);
    ASSERT_EQ(qup->_exclusive,false);
    ASSERT_EQ(qup->_auto_delete,false);
    ASSERT_EQ(qup->getArgs(),std::string("k2=v2&k1=v1&"));

}

TEST(exchange_test,remove_test)
{
    mqp->deleteMsgQueue("queue2");
    pprmq::MsgQueue::ptr qup = mqp->selectMsgQueue("queue2");
    ASSERT_EQ(qup.get(),nullptr); // 删除了exchange2，此时就拿不到，应该是空
    ASSERT_EQ(mqp->exists("queue2"),false);
}

int main(int argc,char* argv[])
{
    testing::InitGoogleTest(&argc,argv);
    testing::AddGlobalTestEnvironment(new QueueTest);
    return RUN_ALL_TESTS();
}